﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CardInChina.Common.Tool;
using System.Reflection;

namespace CardInChina.Bussiness.Frontend.Models
{
    /// <summary>
    /// 第三方支付主要利用跨域post的方式，各自的参数也不同。此基类定义了公共属性，和转化Html时的反射代码
    /// </summary>
    public abstract class PayInterfaceModel
    {
        public string Action { get; set; }
        public string Method { get; set; }
        public string Charset { get; set; }        

        protected PayInterfaceModel(string action, string method, string charset)
        {
            Action = action;
            Method = method;
            Charset = charset;
        }

        protected PayInterfaceModel(string action) : this(action, null, null) { }

        protected PayInterfaceModel(string action, string method) : this(action, method, null) { }


        public virtual string Render()
        {
            StringBuilder sb = new StringBuilder();
            if (string.IsNullOrEmpty(Action))
                throw new Exception("Action must not be null or empty");
            sb.Append(string.Format("<form action=\"{0}\" ",HtmlHelper.HtmlEncode(Action)));
            if(string.IsNullOrEmpty(Method))
                sb.Append("method=\"post\" ");
            else
                sb.Append(string.Format("method=\"{0}\" ",Method));

            if(!string.IsNullOrEmpty(Charset))
                sb.Append(string.Format("charset=\"{0}\" ", Charset));

            sb.Append(">");


            RenderField(sb);

            RenderSubmit(sb);

            sb.Append("</form>");
            return sb.ToString();
        }

        protected virtual void RenderField(StringBuilder sb)
        {
            var propertyInfoes = RefelectProperty(this.GetType());
            foreach (PropertyInfo pi in propertyInfoes)
            {
                object attr = pi.GetCustomAttributes(typeof(FieldAttribute), false).FirstOrDefault();
                if (attr != null && attr is FieldAttribute)
                {
                    bool unhide = ((FieldAttribute)attr).UnHide;
                    string name = ((FieldAttribute)attr).Name;
                    object value = pi.GetValue(this, null);

                    if(!unhide)
                        sb.Append("<input type=\"text\" ");
                    else
                        sb.Append("<input type=\"hidden\" ");

                    sb.Append(string.Format("name=\"{0}\" ",name));

                    if (value == null)
                        sb.Append("value=\"\"/>");
                    else
                        sb.Append(string.Format("value=\"{0}\"/>",HtmlHelper.HtmlEncode(value.ToString())));
                }
            }
        

        }

        private PropertyInfo[] RefelectProperty(Type targetType)
        {
            PropertyInfo[] propertyInfoes = targetType.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            return propertyInfoes;
        }


        protected abstract void RenderSubmit(StringBuilder sb);
    }
}
